package com.imooc.controller;

import com.imooc.pojo.bo.ShopcartBO;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.JsonUtils;
import com.imooc.utils.RedisOperator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Api(value = "购物车接口controller", tags = {"购物车接口相关的api"})
@RestController
@RequestMapping("shopcart")
public class ShopcatController extends BaseController {

    @Autowired
    private RedisOperator redisOperator;

    @ApiOperation(value = "添加商品到购物车", notes = "添加商品到购物车", httpMethod = "POST")
    @PostMapping("/add")
    public IMOOCJSONResult add(@RequestParam String userId, @RequestBody ShopcartBO shopcartBO, HttpServletRequest request,
                               HttpServletResponse response) {
        //由于我们涉及到cookie,会有request和response
        //HAVETODO 用户登录后,会访问这个接口,就会涉及权限
        // 和redis分布式会话有关,还涉及拦截器,但此时是第一阶段,我们先做一个简单的userId判空即可
        if (StringUtils.isBlank(userId)) {
            return IMOOCJSONResult.errorMsg("");
        }
        System.out.println(shopcartBO);
        //前端用户在登录的情况下,添加商品到购物车,会同时在后端同步购物车带redis缓存
        //通过RedisOperator获取购物车数据
        String shopcartJson = redisOperator.get(FOODIE_SHOPCART + ":" + userId);
        List<ShopcartBO> shopcartList = null;
        if (StringUtils.isNotBlank(shopcartJson)) {
            //redis中有购物车
            shopcartList = JsonUtils.jsonToList(shopcartJson, ShopcartBO.class);
            boolean isHaving = false;
            for (ShopcartBO sc : shopcartList) {
                String tmpSpecId = sc.getSpecId();
                //如果已经有类似内容了,已经存在购物车
                if (tmpSpecId.equals(shopcartBO.getSpecId())) {
                    //已经有相同内容只需要进行数量叠加
                    sc.setBuyCounts(sc.getBuyCounts() + shopcartBO.getBuyCounts());
                    isHaving = true;
                }
            }
            if (!isHaving) {
                shopcartList.add(shopcartBO);
            }
        } else {
            shopcartList = new ArrayList<>();
            shopcartList.add(shopcartBO);
        }
        redisOperator.set(FOODIE_SHOPCART + ":" + userId, JsonUtils.objectToJson(shopcartList));
        return IMOOCJSONResult.ok();
    }

    @ApiOperation(value = "删除购物车的商品", notes = "删除购物车的商品", httpMethod = "POST")
    @PostMapping("/del")
    public IMOOCJSONResult del(@RequestParam String userId, @RequestParam String itemSpecId) {
        //由于我们涉及到cookie,会有request和response
        //HAVETODO 用户登录后,会访问这个接口,就会涉及权限,
        // 和redis分布式会话有关,还涉及拦截器,但此时是第一阶段,我们先做一个简单的userId判空即可
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(itemSpecId)) {
            return IMOOCJSONResult.errorMsg("参数不能为空");
        }
        //  用户在页面删除购物车的商品数据,没有登录覆盖cookie,有登录则删除redis购物车数据
        String shopcartJson = redisOperator.get(FOODIE_SHOPCART + ":" + userId);
        if (!StringUtils.isBlank(shopcartJson)) {
            //redis有购物车这需要循环遍历查找删除
            //首先String转list
            List<ShopcartBO> shopcartList = JsonUtils.jsonToList(shopcartJson, ShopcartBO.class);
            for (ShopcartBO sc : shopcartList) {
                String tmpSpecId = sc.getSpecId();
                if (tmpSpecId.equals(itemSpecId)) {
                    //如果找到了就直接break不需要找了
                    shopcartList.remove(sc);
                    break;
                }
            }
            //处理完毕则进行redis的缓存覆盖,因为你无法删除redis的单个元素
            redisOperator.set(FOODIE_SHOPCART + ":" + userId, JsonUtils.objectToJson(shopcartList));
        }

        return IMOOCJSONResult.ok();
    }
}